﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.Entity;

public partial class Chain_Maintain : PageBase
{
    private int id=0;
    private const string CHANNEL_ID = "id";
    private const string RETURN_PAGE = "Chain_List.aspx";
    private const string RESPONSE_NEW_BANNER = "Banner_Maintain.aspx?chainID=";
    private OrgainzationManager orgManager = new OrgainzationManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[CHANNEL_ID] != null)
        {
            int.TryParse(Request.QueryString[CHANNEL_ID], out id);
            if (id > 0)
            {
                txtCode.Enabled = false;
                txtName.Enabled = false;
            }
        }
        if (!IsPostBack)
        {
            DataBind();
            this.FillForm();
            
        }
    }

    private void FillForm()
    {
        if (id>0)
        {
            CHAINS level = new ChainManager().Load(id);
            if (level != null)
            {

                DataTable dtOrgs = new ChainManager().LoadOrg(id);
                foreach (DataRow row in dtOrgs.Rows)
                {
                    txtOrg.Text += row["NAME"].ToString() + ",";
                    txtHidOrg.Value += row["ORG_ID"].ToString() + ",";
                }
                txtOrg.Text = txtOrg.Text.TrimEnd(',');
                txtHidOrg.Value = txtHidOrg.Value.TrimEnd(',');
                txtName.Text = level.DESCRIPTION;
                txtCode.Text = level.CODE;
            }
        }
    }

    private int SaveData()
    {
        CHAINS level = null;
        ChainManager chainManager = new ChainManager();
        decimal newID=0;

        if (id > 0)
        {
            level = chainManager.Load(id);
        }
        else
        {
            level = new CHAINS();

            if (chainManager.LoadCode(txtCode.Text, MyStateManager.DomainID) != null)
            {
                ShowMessage("已存在此代码");
                return 0;
            }
        }
        level.CODE = txtCode.Text;
        level.DESCRIPTION = txtName.Text;
        level.DOMAINID = this.MyStateManager.DomainID;
        level.LAST_UPDATER = this.MyStateManager.Name;
        level.LAST_UPDATE_TIME = long.Parse(Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeString(DateTime.Now));

        newID=chainManager.Save(level);

        //删除与该经销商的部门
        chainManager.DelteOrgsByChainID(level.ID);

        string[] orgs = txtHidOrg.Value.Split(',', '\'');
        foreach (string org in orgs)
        {
            if (org == string.Empty) continue;
            CHAINS_ORG chainsOrg = new CHAINS_ORG();
            chainsOrg.CHAINID = level.ID;
            chainsOrg.ORGID = int.Parse(org);
            chainsOrg.LAST_UPDATE_TIME = DateTime.Now;
            //chainsOrg.LAST_UPDATER = MyStateManager.Name;
            chainsOrg.Save();
        }
        

        return id == 0 ? int.Parse(newID.ToString()) : id;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        decimal id = SaveData();

        if (id != 0 )
        {
            Response.Redirect(RETURN_PAGE);
        }
        
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(RETURN_PAGE);
    }
}
